package com.ruis.kotboot.config.confs

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.support.config.FastJsonConfig
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import java.util.ArrayList

@Configuration
@ConditionalOnClass(FastJsonHttpMessageConverter::class) //1
@ConditionalOnProperty(//2
        name = ["spring.http.converters.preferred-json-mapper"],
        havingValue = "fastjson",
        matchIfMissing = true
)
class FastJsonCvtConfiguration {

    @Bean
    @ConditionalOnMissingBean(FastJsonHttpMessageConverter::class)
    fun fastJsonHttpMessageConverter():FastJsonHttpMessageConverter{
        val converter = FastJsonHttpMessageConverter()
        val fastJsonConfig = FastJsonConfig()
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.PrettyFormat
                , SerializerFeature.DisableCircularReferenceDetect
        )
        //日期格式化
        fastJsonConfig.dateFormat = "yyyy-MM-dd HH:mm:ss"
        converter.fastJsonConfig = fastJsonConfig

        val supportedMediaTypes = ArrayList<MediaType>()
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8)
        converter.supportedMediaTypes=supportedMediaTypes
        return converter
    }
}